package com.py.custom.business.demo.core.controller;

import com.py.custom.business.demo.core.mapstruct.BeanConvertUtils;
import com.py.custom.business.demo.core.service.TemplateService;
import com.py.custom.business.demo.core.service.model.TemplatePageDTO;
import com.py.custom.business.demo.protocol.groups.StudentGroup;
import com.py.custom.business.demo.protocol.groups.TemplateGroup;
import com.py.custom.business.demo.protocol.request.*;
import com.py.custom.business.demo.protocol.response.TemplateResponse;
import com.py.custom.business.demo.protocol.vo.TemplateVO;
import com.py.custom.common.web.response.BaseResponse;
import com.py.custom.common.web.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * description： 消息模板 管理：查询、创建、修改、删除
 */
@RestController
@RequestMapping("/template")
public class TemplateController {

    @Autowired
    private BeanConvertUtils beanConvertUtils;

    @Resource
    private TemplateService templateService;

    @PostMapping("/query")
    public BaseResponse<TemplateResponse> query(@Validated(value = {TemplateGroup.QueryGroup.class})
                                                @RequestBody TemplateRequest request) {
        TemplateResponse templateResponse = new TemplateResponse();
        // 查询
        TemplatePageDTO templatePageDto = templateService.query(request);
        // 转换
        List<TemplateVO> templateVoList = beanConvertUtils.templateDto2VoList(templatePageDto.getList());

        // 赋值
        templateResponse.setList(templateVoList);
        templateResponse.setTotal(templatePageDto.getTotal());
        return ResponseUtils.success(templateResponse);
    }

    @PostMapping("/add")
    @ResponseBody
    public BaseResponse<Boolean> add(@Validated(value = {TemplateGroup.SaveGroup.class})
                                     @RequestBody AddTemplateRequest request) {
        boolean hasSave = templateService.addTemplate(request);
        return ResponseUtils.success(hasSave);
    }

    @PostMapping("/update")
    @ResponseBody
    public BaseResponse<Boolean> update(@Validated(value = {TemplateGroup.UpdateGroup.class})
                                        @RequestBody UpdateTemplateRequest request) {
        boolean updateTemplate = templateService.updateTemplate(request);
        return ResponseUtils.success(updateTemplate);
    }

    @PostMapping("/delete")
    @ResponseBody
    public BaseResponse<Boolean> delete(@Validated(value = {TemplateGroup.DelGroup.class})
                                        @RequestBody DelTemplateRequest request) {
        boolean delTemplate = templateService.delTemplate(request);
        return ResponseUtils.success(delTemplate);
    }
}
